package com.gzy.gameserver;

import java.io.Serializable;
import java.awt.geom.Point2D;

public class Dir2D 
	implements Serializable
{
	private static final long serialVersionUID 	= 1L;
	
	public static final byte front_dir			= 1;
	public static final byte right_front_dir	= 2;
	public static final byte right_dir			= 3;
	public static final byte right_back_dir		= 4;
	public static final byte back_dir			= 5;
	public static final byte left_back_dir		= 6;
	public static final byte left_dir			= 7;
	public static final byte left_front_dir		= 8;

	private byte dir = front_dir;
	Point2D.Float dir2D = new Point2D.Float(0, 0);
	
	public Dir2D(byte dir)
	{
		this.dir = dir;
	}
	Dir2D( Point2D.Float dir)
	{
		this.setDir(dir);
	}

	void setDir(Point2D.Float dir)
	{
		this.dir2D = Point2DOp.normalize(dir);

		double pi2 = Math.PI*2.f;
		double angle = Math.acos(Point2DOp.dot(this.dir2D, new Point2D.Float(0.f, -1.f)));
		if(this.dir2D.x < 0)
		{
			angle = pi2 - angle;
		}

		angle = (angle + Math.PI/4.0/2.0);

		this.dir = (byte)(int)((angle>pi2?(angle-pi2):angle)/(Math.PI/4.0)+1);
	}
	byte getDir()
	{ 
		return this.dir; 
	}
	Point2D.Float getDir2D()
	{ 
		return this.dir2D; 
	}
};
